package de.dopichaj.labrador.search.hit;


import java.io.File;

import de.dopichaj.labrador.index.Tag;

/**
 * A chained hit that is an inner hit (i.e. not the root).
 */
public final class InnerChainedHit extends AbstractHit implements ChainedHit {

    private final ChainedHit parent;
    private final int position;
    private final int contentLength;
    private final Tag tag;
    private final int xPathPosition;

    public InnerChainedHit(final ChainedHit parent, final int contentLength,
        final int position, final Tag tag, final int xPathPosition) {
        
        assert parent != null;
        this.parent = parent;
        this.position = position;
        this.contentLength = contentLength;
        this.tag = tag;
        this.xPathPosition = xPathPosition;
    }
    
    public int getContentLength() {
        return contentLength;
    }

    public int getDepth() {
        return parent.getDepth() + 1;
    }

    public File getFile() {
        return parent.getFile();
    }

    public int getPosition() {
        return position;
    }
    
    private String getXPathPart() {
        return "/" + tag.getName() + "[" + xPathPosition + "]";
    }

    public String getXPath() {
        return parent.getXPath() + getXPathPart();
    }

    public String[] getXPathParts() {
        final String[] parentXPathParts = parent.getXPathParts();
        final String[] xPathParts = new String[parentXPathParts.length + 1];
        for (int i = 0; i < parentXPathParts.length; i++) {
            xPathParts[i] = parentXPathParts[i];
        }
        xPathParts[xPathParts.length - 1] = getXPathPart();
        return xPathParts;
    }

    public ChainedHit getParent() {
        return parent;
    }

    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = position;
        result = PRIME * result + tag.hashCode();
        result = PRIME * result + xPathPosition;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (getClass() != obj.getClass())
            return false;
        final InnerChainedHit other = (InnerChainedHit)obj;
        if (position != other.position)
            return false;
        if (!tag.equals(other.tag))
            return false;
        if (xPathPosition != other.xPathPosition)
            return false;
        return parent.equals(other.parent);
    }

}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/